#include<opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
 

 Mat mask;
int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 4;
 vector<Vec4i> hierarchy;

 void Erosion( int, void* );
 void Dilation(int, void* );
//  void Dilation( int, void* );
int main(int argv, char** argc)
{

Ptr<BackgroundSubtractor> pBackSub;
   
        pBackSub = createBackgroundSubtractorMOG2();
	VideoCapture capture("./water.avi");
    if (!capture.isOpened()){
        //error in opening the video input
        printf("could not load image..\n");
        return false;
    }
    Mat frame, hsv,fgMask;
    namedWindow( "Frame", WINDOW_AUTOSIZE );
    namedWindow( "FG Mask", WINDOW_AUTOSIZE );
        //show the current frame and the fg masks
        
    createTrackbar( "erosion:", "FG Mask",&erosion_size, max_elem,Erosion);
    createTrackbar( "dilation:", "FG Mask",&dilation_size, max_elem,Dilation);
    while (true) {
        capture >> frame;
        // frame = imutils.resize(frame, 600)
        GaussianBlur(frame,frame, Size(11, 11),3,3);
        // Laplacian(frame,frame,0)
        // blurred = GaussianBlur(frame, (11, 11), 0)
        if (frame.empty())
            break;
    
	cvtColor(frame, hsv, COLOR_BGR2HSV);
inRange(hsv,Scalar(0, 43, 46),Scalar(26, 255, 255),mask);
Erosion(0,0);
Dilation(0,0);
// erode(mask,mask, Mat(), Point(-1,-1), 2);
// dilate(mask,mask, Mat(), Point(-1,-1), 2);
    vector<vector<Point> > cnts;
    findContours(mask,cnts,hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// vector<Point2f> cnt=cnts[0];
pBackSub->apply(mask, fgMask);
        // //get the frame number and write it on the current frame
        // rectangle(mask, cv::Point(10, 2), cv::Point(100,20),
        //           cv::Scalar(255,255,255), -1);
        // stringstream ss;
        // ss << capture.get(CAP_PROP_POS_FRAMES);
        // string frameNumberString = ss.str();
        // putText(mask, frameNumberString.c_str(), cv::Point(15, 15),
        //         FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));

//    if len(cnts) > 100000 and len(cnts) < 200000:
    // if len(cnts) > 0:
    // // #cv.boundingRect()返回轮廓矩阵的坐标值，四个值为x, y, w, h， 其中x, y为左上角坐标，w,h为矩阵的宽和高
    //     boxes = [boundingRect(c) for c in cnts]
    //     for box in boxes:
    //         x, y, w, h = box
    //     // #绘制矩形框对轮廓进行定位
    //         if w>13 and h>13 and w<70 and h<70:
    //             origin_pic = cv.rectangle(mask1, (x, y), (x+w, y+h), (153, 153, 0), 2)
             
    // dst=cornerHarris(mask1,2,3,0.04)
    // frame[dst>0.01*dst.max()]=[0,255,0]
	// Rect rRect = boundingRect(cnts);
	// rectangle(frame,Point(rRect.x, rRect.y), Point(rRect.x+ rRect.width, rRect.y + rRect.height),Scalar(0,0,255),2,8,0);
  vector<vector<Point> >::const_iterator itContours = cnts.begin();
            for (int c=0; itContours != cnts.end(); ++itContours){
                if (itContours->size() >= 50){
                    Rect r = boundingRect(cnts[c]);
                    rectangle(frame, r, Scalar(0,255,0), 2);
                }

                c++;
            }


        imshow("Frame", frame);
        imshow("FG Mask", fgMask);
        //get the input from the keyboard
        int keyboard = waitKey(10);
        if (keyboard == 'q' || keyboard == 27)
            break;
    }
	return(0);
 
}

void Erosion( int, void* )
{
  int erosion_type = 0;
  if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
  else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
  else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
  Mat element = getStructuringElement( erosion_type,
                       Size( 2*erosion_size + 1, 2*erosion_size+1 ),
                       Point( erosion_size, erosion_size ) );
  erode( mask, mask, element );
}

void Dilation(int, void* )
{
  int dilation_type = 0;
  if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
  else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
  else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
  Mat element = getStructuringElement( dilation_type,
                       Size( 2*dilation_size + 1, 2*dilation_size+1 ),
                       Point( dilation_size, dilation_size ) );
  dilate( mask,mask, element );
}